package cn.engage.org.controller;

import cn.engage.basic.anno.LogAnno;
import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import cn.engage.org.domain.Systemdictionarytype;
import cn.engage.org.query.SystemdictionarytypeQuery;
import cn.engage.org.service.SystemdictionarydetailService;
import cn.engage.org.service.SystemdictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class SystemdictionarytypeController {
    @Autowired
    private SystemdictionarytypeService systemdictionarytypeService;
    @Autowired
    private SystemdictionarydetailService systemdictionarydetailService;
    // 查询所有
    @GetMapping
    @LogAnno(method = "findAll",methodDes = "查询所有字典类型")
    public List<Systemdictionarytype> findAll(){
        return systemdictionarytypeService.findAll();
    }

    // 查询单个
    @GetMapping("/{id}")
    @LogAnno(method = "findById",methodDes = "通过id查询字典类型")
    public List<Systemdictionarytype> findById(@PathVariable("id") Long id){
        return systemdictionarytypeService.findById(id);
    }

    // 分页查询
    @PostMapping
    @LogAnno(method = "queryByPage",methodDes = "分页查询字典类型")
    public PageList<Systemdictionarytype> queryByPage(@RequestBody SystemdictionarytypeQuery query){
        return systemdictionarytypeService.queryByPage(query);
    }

    // 新增和修改
    @PutMapping
    @LogAnno(method = "addOrUpdate",methodDes = "新增修改字典类型")
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarytype systemdictionarytype){
        if (null == systemdictionarytype.getId()){
            // 添加
            systemdictionarytypeService.add(systemdictionarytype);
        }else {
            // 修改
            systemdictionarytypeService.update(systemdictionarytype);
        }
        return AjaxResult.me().success();
    }

    // 删除
    @DeleteMapping("/{id}")
    @LogAnno(method = "delete",methodDes = "删除字典类型")
    public AjaxResult delete(@PathVariable("id") Long id){
        systemdictionarydetailService.delete2(id);
        systemdictionarytypeService.delete(id);

        return AjaxResult.me().success();
    }

    // 批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        systemdictionarydetailService.patchDelete2(ids);
        systemdictionarytypeService.patchDelete(ids);

        return AjaxResult.me().success();
    }
}
